package com.hoongs.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoongs.v1.entity.Article;
import com.hoongs.v1.entity.Tag;
import com.hoongs.v1.mapper.TagMapper;
import com.hoongs.v1.service.TagService;
import com.hoongs.v1.utils.PResult;
import com.hoongs.v1.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    public static final String NOT_FOUND_ARTICLE = "没有找到该标签！";

    @Resource
    private TagMapper tagMapper;

    @Override
    public Result save(Tag tag) {
        if (StringUtils.isNotBlank(tag.getId())) {
            this.update(tag);
        } else {
            this.add(tag);
        }
        return null;
    }

    @Override
    public Result add(Tag tag) {
        if (tagMapper.insert(tag) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
        if (!findTagById(id)) return Result.opError(NOT_FOUND_ARTICLE);
        if (tagMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(Tag tag) {
        if (!findTagById(tag.getId())) return Result.opError(NOT_FOUND_ARTICLE);
        if (tagMapper.updateById(tag) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result selectById(String id) {
        Tag tag = tagMapper.selectById(id);
        if (tag == null) return Result.error(NOT_FOUND_ARTICLE);
        return Result.success(tag);
    }

    @Override
    public Result selectList(String title, String status) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<Tag>();
        if (StringUtils.isNotBlank(title)) {
            wrapper.like("title", title);
        }
        if (StringUtils.isNotBlank(status)) {
            wrapper.eq("status", status);
        }
        Collection list = tagMapper.selectList(wrapper);
        return Result.success(list);
    }

    @Override
    public Result selectPageList(Integer page, Integer size) {
        if (!checkPageQuery(page, size)) return Result.error("分页参数错误");
        Page<Tag> daoPage = new Page<>(page, size);
        IPage<Tag> tagDaoIPage = tagMapper.selectPage(daoPage, null);
        PResult rs = new PResult(tagDaoIPage);
        return Result.success(rs);
    }

    /**
     * 根据Id查询用户是否存在
     */
    public Boolean findTagById(String id) {
        Tag tag = tagMapper.selectById(id);
        return tag != null;
    }

    /**
     * 检查分页参数是否为空
     */
    public Boolean checkPageQuery(Integer page, Integer size) {
        return page != null && page > 0 && size != null && size > 0;
    }

}
